// License Notification
//======================================================
// Copyright (c) 2010 Channel Intelligence Inc.
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// ======================================================




using System;
using System.IO;
using System.Collections.Generic;
using CIP4.Common;

namespace CIP4.Simple
{
    public class CursorType: IDisposable
    {
        /// <summary>
        /// Gets a serializer of a specified kind
        /// </summary>
        public GetSerializerDelegate GetSerializer { get; set; }
        /// <summary>
        /// If this is true, the provider that returns results for the cursor will throw and error if the search is linear.
        /// This applies to purge as well as get. You should set it to false if you intend an access to result in a linear search.
        /// </summary>
        public bool FailOnLinearSearch { get; set; }

        /// <summary>
        /// Used by some providers as a place marker (i.e. where the last access left off)
        /// </summary>
        public long PageNumber { get; set; }

        /// <summary>
        /// The number of items the provider is expected to get in a batch - the provider may return less but should not return more
        /// </summary>
        public int PageSize { get; set; }

        /// <summary>
        /// Used by some providers as a continuation marker (i.e. where the last access left off)
        /// </summary>
        public string Marker { get; set; }

        /// <summary>
        /// The number of records seen by this cursor so far
        /// </summary>
        public long RecordCount { get; set; }

        /// <summary>
        /// Used to report errors and other information that arise while executing this cursor
        /// </summary>
        public LoggerDelegate Logger { get; set; }

        /// <summary>
        /// On each call to GetItems, any streams in this dictionary will be closed and discarded.
        /// When an item is fetched by GetItems, if the item has a stream property, if the stream
        /// property has a value, it will be initialized and placed in this list. i.e. streams
        /// for stream properties cannot be held open from one item to the next in the same iteration.
        /// The dictionary string value is the path for the stream used by the store to find the data.
        /// ToDo: This isn't implemented yet - currently relying on GC dispose to get rid of the streams
        /// </summary>
        public Dictionary<Stream, string> Streams { get; set; }

        internal CursorType(GetSerializerDelegate aGetSerializer)
        {
            GetSerializer = aGetSerializer;
        }

        #region IDisposable Members

        public void Dispose()
        {
            foreach(Stream stream in Streams.Keys)
                stream.Dispose();
        }

        #endregion
    }
}
